package com.huanzhu.asuper.supervisionapp.cjbjdudao.view;

import android.app.Dialog;
import android.content.Context;
import android.os.Handler;
import android.os.Message;
import android.view.animation.Animation;
import android.view.animation.AnimationUtils;
import android.view.animation.LinearInterpolator;
import android.widget.ImageView;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.core.content.ContextCompat;

import com.huanzhu.asuper.supervisionapp.cjbjdudao.R;
import com.huanzhu.asuper.supervisionapp.cjbjdudao.uitls.GlideUtils;

/**
 * created by lihao on 2018/10/16
 */
public class SVProgressDialog extends Dialog {
    private ImageView mHudIv;
    private TextView mHudTv;
    private static Animation mOperatingAnim;
    //显示时间
    private static final long DISMISSDELAYED = 1500;

    public SVProgressDialog(@NonNull Context context) {
        super(context, R.style.SVProgressDialog);
        setContentView(R.layout.dialog_svprogress_hud);
        setCanceledOnTouchOutside(false);
        mHudIv = findViewById(R.id.hud_iv);
        mHudTv = findViewById(R.id.hud_tv);
    }

    /**
     * 开始旋转动画
     */
    private void startAnim() {
        if (mOperatingAnim == null) {
            mOperatingAnim = AnimationUtils.loadAnimation(getContext(), R.anim.hud_rotate);
            LinearInterpolator lin = new LinearInterpolator();
            mOperatingAnim.setInterpolator(lin);
        }
        mHudIv.startAnimation(mOperatingAnim);
    }

    /**
     * 停止旋转动画
     */
    private void stopAnim() {
        mHudIv.clearAnimation();
    }

    /**
     * 显示时间计时
     */
    private void scheduleDismiss() {
        mHandler.removeCallbacksAndMessages(null);
        mHandler.sendEmptyMessageDelayed(0, DISMISSDELAYED);
    }

    private Handler mHandler = new Handler() {
        @Override
        public void handleMessage(Message msg) {
            super.handleMessage(msg);
//            stopAnim();
            mHudIv.setImageDrawable(null);
            dismiss();
        }
    };

    /**
     * 进度条+文字-场景：网络请求等
     */
    public void showWithStatus(String string) {
        if (!isShowing())
            show();
        mHandler.removeCallbacksAndMessages(null);
        mHudIv.setImageDrawable(ContextCompat.getDrawable(getContext(), R.mipmap.ic_svstatus_loading));
//        startAnim();
        GlideUtils.loadImageView(getContext(), R.drawable.gif_loading, mHudIv);
        mHudTv.setText(string);
    }

    /**
     * 短暂提示功能
     *
     * @param info 提示信息
     */
    public void showInfoWithStatus(String info) {
        if (!isShowing())
            show();
        stopAnim();
//        mHudIv.setImageDrawable(ContextCompat.getDrawable(getContext(), R.mipmap.ic_svstatus_info));
        mHudTv.setText(info);
        scheduleDismiss();
    }

    /**
     * 短暂提示-比如：提交成功
     *
     * @param info 提示信息
     */
    public void showSuccessWithStatus(String info) {
        if (!isShowing())
        show();
        stopAnim();
        mHudIv.setImageDrawable(ContextCompat.getDrawable(getContext(), R.mipmap.success_page));
        mHudTv.setText(info);
        scheduleDismiss();
    }

    /**
     * 短暂提示-比如：提交失败
     *
     * @param info 提示信息
     */
    public void showErrorWithStatus(String info) {
        if (!isShowing())
            show();
        stopAnim();
        mHudIv.setImageDrawable(ContextCompat.getDrawable(getContext(), R.mipmap.error_page));
        mHudTv.setText(info);
        scheduleDismiss();
    }

    /**
     * 消失
     */
    @Override
    public void dismiss() {
        if (isShowing()) {
            mHandler.removeCallbacksAndMessages(null);
            super.dismiss();
        }
    }
}
